package com.search.service.impl;
import java.util.List;

import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson2.JSONObject;
import com.search.service.DoubanBookService;
import com.search.dao.DoubanBookDao;
import com.search.entity.DoubanBook;
/**
 * DoubanBook的服务接口的实现类
 * 
 * @author 
 *
 */
@Slf4j
@Service
public class DoubanBookServiceImpl implements DoubanBookService {
	private final Logger LOG = LogManager.getLogger(this.getClass());

	@Autowired
	private DoubanBookDao doubanBookDao;
	// TODO 当你看到这个方法时你应该创建一个工具类做通用的方法,定义自己的返回格式化
	private static final int C200 = 200;
	private static final int C412 = 412;
	public String resultFormat(int code, Object data) {
		JSONObject result = new JSONObject();
		result.put("code", code);
		if (data != null) {
			result.put("data", data);
		}
		return result.toJSONString();
	}

	@Override
	public List<DoubanBook> find(DoubanBook value) {
		//TODO这里可以做通过Assist做添加查询
		if(0==value.getOffset()){
			value.setOffset(10);
		}
		if(0==value.getPage()){
			value.setPage(1);
		}
		value.setBegin((value.getPage()-1)*value.getOffset());
		List<DoubanBook> result = doubanBookDao.selectDoubanBookByObj(value);
		if (LOG.isDebugEnabled()) {
			LOG.debug("执行获取DoubanBook数据集-->结果:", result);
		}
		return  result;
	}
	@Override
	public DoubanBook findOne(String id) {
		if (id == null) {
			if (LOG.isDebugEnabled()) {
				LOG.debug("执行通过DoubanBook的id获得DoubanBook对象-->失败:id不能为空");
			}
			return null;
		}
		DoubanBook result = doubanBookDao.selectDoubanBookById(id);
		if (LOG.isDebugEnabled()) {
			LOG.debug("执行通过DoubanBook的id获得DoubanBook对象-->结果:", result);
		}
		return result;
	}

	@Override
	@Async
	public String saveNotNull(DoubanBook value) {
		if (value == null) {
			if (LOG.isDebugEnabled()) {
				LOG.debug("执行将DoubanBook中属性值不为null的数据保存到数据库-->失败:对象不能为空");
			}
			return resultFormat(C412, null);
		}
		int result = -1;
		try{
			 result = doubanBookDao.insertNotNullDoubanBook(value);
		}catch (Exception e){
			//e.printStackTrace();
			log.error("doubanBook插入出错"+e.getCause()+value.toString());
		}

		if (LOG.isDebugEnabled()) {
			LOG.debug("执行将DoubanBook中属性值不为null的数据保存到数据库-->结果:", result);
		}
		return resultFormat(C200, result);
	}
	@Override
	public String updateNotNullById(DoubanBook value) {
		if (value == null) {
			if (LOG.isDebugEnabled()) {
				LOG.debug("执行通过DoubanBook的id更新DoubanBook中属性不为null的数据-->失败:对象为null");
			}
			return resultFormat(C412, null);
		}
		int result = doubanBookDao.updateNotNullDoubanBookById(value);
		if (LOG.isDebugEnabled()) {
			LOG.debug("执行通过DoubanBook的id更新DoubanBook中属性不为null的数据-->结果:", result);
		}
		return resultFormat(C200, result);
	}

	@Override
	public String deleteById(String id) {
		if (id == null) {
			if (LOG.isDebugEnabled()) {
				LOG.debug("执行通过DoubanBook的id删除DoubanBook-->失败:id不能为空");
			}
			return resultFormat(C412, null);
		}
		int result = doubanBookDao.deleteDoubanBookById(id);
		if (LOG.isDebugEnabled()) {
			LOG.debug("执行通过DoubanBook的id删除DoubanBook-->结果:", result);
		}
		return resultFormat(C200, result);
	}

	@Override
	public int count() {
		return 0;
	}


}